package leetcode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;

/**
 * @author Retain
 * @date 2021/9/11 15:57
 */
public class MeiTuan {
    public static void main(String[] args) throws IOException {
        q2();
    }

    public static void q1() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int n = s.length();
        int count = 0;

        for (int i = 0; i < n; i++) {
            for (int j = i; j < n; j++) {
                BigInteger m;
                m = BigInteger.valueOf(Long.parseLong(s.substring(i, j + 1)));
                BigInteger[] integers = m.divideAndRemainder(BigInteger.valueOf(22));
                if (integers[1].equals(BigInteger.valueOf(0))) {
                    System.out.println(i + " " + j +Integer.parseInt(s.substring(i, j + 1)));
                    count++;
                }
            }
        }
        System.out.println(count);
    }

    public static void q2() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int[] s = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        int n = s[0];
        int k = s[1];
        int[] a = new int[n];
        int[] b = new int[n];
        int[] c = new int[n];
        int[] d = new int[n];
        for (int i = 0; i < n; i++) {
            int[] song = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
            a[i] = song[0];
            b[i] = song[1];
            c[i] = song[2];
            d[i] = song[3];
        }
        System.out.println();
        HashSet<Integer> set = new HashSet<>();
        int res = 0;
        int max = Arrays.stream(b).max().getAsInt();
        for (int i = 0; i < n; i++) {
            // 有没有听前一首歌
            int nowSong = c[i] * 10 + d[i];
            int serial = nowSong - 1;
            // 第一首歌，可以听
            if (d[i] == 1) {
                set.add(nowSong);
                res += a[i];
                // 非第一首歌且降低值最大
            }else if (b[i] == max){
                res -= k;
                // 非第一首歌但降低值还好
            } else {
                res += a[i];
                // 如果没有听前一首歌

                while (serial % 10 > 0) {
                    if (!set.contains(serial)) {
                        // 减少愉悦值
                        res -= b[i];
                        break;
                    }
                    serial--;
                }
                set.add(nowSong);
            }
        }
        System.out.println(res);
    }

    public static void q3() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] s = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        System.out.println(1);
    }
}
